package com.wissen.ajaxservlet.client;
/**
 * @author wissen16(Mayur Birari)
 */
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.io.*;

/**
 * @author wissen16(Mayur Birari)
 */
@SuppressWarnings("serial")
public class GetServlet extends HttpServlet {

	String username = "root";
	String password = "wissen";
	Connection con;
	ResultSet rs;
	PreparedStatement stmt;
	Statement stmt1;
	String url = "jdbc:mysql://localhost:3306/google";
	PrintWriter pw1;
/**
 * to get initialise the connection objects
 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(url, username, password);
			// stmt = con.createStatement();
		} catch (Exception e) {
			System.out.println("Sql Error" + e.getMessage());
		}
	}

	/**
	 * to get request handle for requested page
	 */
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		pw1 = resp.getWriter();
		resp.setContentType("text/html");
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(url, username, password);
			// stmt = con.createStatement();
			if(req.getParameter("hidden").equals("insert")){
				String sql = "insert into account values(";
				sql += "'" + req.getParameter("cfname") + "',";
				sql += "'" + req.getParameter("clname") + "',";
				sql += "'" + req.getParameter("clogn") + "',";
				sql += "'" + req.getParameter("cpwd") + "',";
				sql += "'" + req.getParameter("selq") + "',";
				sql += "'" + req.getParameter("ans") + "',";
				sql += "'" + req.getParameter("sece") + "',";
				sql += "'" + req.getParameter("country") + "')";
				stmt = con.prepareStatement(sql);
				stmt.executeUpdate();
				pw1.write("Account Created SuccessFully<br><b>");
				pw1.write(req.getParameter("cfname")+"</b>");
				}
			
				else if(req.getParameter("hidden").equals("update")){
					String sql = "update account set fname='";
					sql += req.getParameter("ufname") +"',lname='";
					sql += req.getParameter("ulname")+"',logname='";
					sql += req.getParameter("ulogn")+"',pwd='";
					sql += req.getParameter("upwd")+"' where logname='";
					sql += req.getParameter("uname")+"'";
					stmt = con.prepareStatement(sql);
					stmt.executeUpdate();
					pw1.write("Account Updated SuccessFully<br><b>");
					pw1.write(req.getParameter("ufname")+"</b>");
					}
			
				else if(req.getParameter("hidden").equals("delete")){
					String sql = "delete from account where logname='";
					sql += req.getParameter("dname")+"'";
					stmt = con.prepareStatement(sql);
					stmt.executeUpdate();
					pw1.write("Account deleted SuccessFully<br><b>");
					pw1.write(req.getParameter("dname")+"</b>");
					}
			
			con.close();
			//imp -http://projects.allmarkedup.com/jquery_url_parser/
			//imp -resp.sendRedirect("Editgmail.html?fname="+req.getParameter("fname")+"&lname="+req.getParameter("lname")+"&logn="+req.getParameter("lid")+"&pwd="+req.getParameter("pwd"));
		} catch (Exception e) {
			pw1.write(e.getMessage());
		}
	}


	/**
	 * to get destroy occupy memory for objects 
	 */
	public void destroy() {
		super.destroy();
	}
}